<?php
namespace app\home\controller;
use \think\Validate;
use \think\Paginator;

class Note extends \think\Controller
{

	public function collecter()
	{
		$note_id = input('get.id');
		$user_id = session('user_id');
		$result = db('collectnote')->where("note_id={$note_id}")->find();
		if($result)
		{
			$this->error("已被收藏");
		}
		else
		{
			$data = [
				'note_id' =>  $note_id,
				'user_id' =>  $user_id,
				'collect_addtime' =>  time()
			];
			$ret = db('collectnote')->insert($data);
		}
		if( $ret ==false )
 		{
 			$this->error("收藏失败");
 		}
 		$this->success("收藏成功");
	}

	public function checkmyNote() //判断是否有点评记录
	{
		$user_id = session('user_id');
		if( $user_id == NULL )
		{
			$this->error("请登录",'home/login/login');
		}
		$result = db('user')->where("user_id={$user_id}")->find();
		$alist = db('note')->alias('a')->join('user b','a.user_id=b.user_id')->where("b.user_id={$user_id}")->where("note_status = 1")->field('a.*,b.user_addtime as time,b.user_img,b.user_name')->order('a.addtime desc')->paginate(10);
		$alist = json_decode(json_encode($alist),true); 
		if(empty($alist['data']))
		{
			$this->error("没有点评记录");
		}
		else
		{
			$this->redirect("/home/note/myNote");
		}
	}

	public function focus()  //收藏帖子
	{
		$user_id = session('user_id');
		$focusnote_list = db('focusnote')->alias('f')
		->join("note n","f.note_id = n.note_id")->where("f.user_id='{$user_id}'")->select();
		foreach($focusnote_list as &$focus){
			$user_info = db('user')->where("user_id = '{$focus["user_id"]}'")->find();
			$focus['user_name'] = $user_info['user_name'];
		}
		// echo "<pre>";
		// var_dump($focusnote_list);die;
		//验证登录代码
        if ( session('user_id') == NULL)
        {	
        		session('url', $_SERVER['PHP_SELF']);
	        	$this->error("未登录",'home/login/login');
   		}
        
        if(!empty($user_id)){
    		$result  = db("user")->where("user_id = {$user_id}")->find();
    	}
    	else{
    		$user_id="";
    		$result=[];
            $result['user_name']="普通用户";
		}

		$hot_list = db("note")->where("note_status = 1")->limit(5)->order('note_visit desc')->select();      //最热帖子列表
        $lastest_list = db("note")->where("note_status = 1")->order("addtime desc")->limit(6)->select();  //最新帖子列表
        $new_list = db("new")->order("addtime desc")->limit(5)->select();       //新闻列表
        $this->assign("hot_list",$hot_list);
        $this->assign("new_list",$new_list);
        $this->assign("lastest_list",$lastest_list);
		
		$user_id = session('user_id');
		if( $user_id == NULL )
		{
			$this->error("请登录",'home/login/login');
		}
		$this->assign('focusnote_list',$focusnote_list);
		$this->assign('user_id',session('user_id'));
        $this->assign("result",$result);
		return view('focus');
	}

	public function notelist()
	{
		$user_id = session('user_id');
		if( $user_id != NULL)
		{
			$result = db('user')->where("user_id={$user_id}")->find();
			$this->assign('user_id',$user_id);
			$this->assign('result',$result);
		}
		else //==NULL
		{
			$user_id = "";
			$result = ['user_name'=>''];
			$this->assign('user_id',$user_id);
			$this->assign('result',$result);
		}

		$alist = db('note')->alias('a')->join('user b','a.user_id=b.user_id')->field('a.*,b.user_name,b.user_img')->where("note_status = 1")->order('addtime desc')->paginate(10);

		$pageHtml = $alist->render();
		$this->assign('alist',$alist);
		$this->assign('pageHtml',$pageHtml);
		return view('note_list');
	}

	public function insertnote()
	{
		header('Content-Type:text/plain;charset=utf-8');
		$note_title = input('post.note_title');
		$note_content = input('post.note_content');
		$note_label = input('post.note_label');
		$file = request()->file('note_himg');//上传文件//获取表单上传文件
		//移动到框架应用根目录/public/uploads/ 目录下
		$img_src = "";
		if($file)
		{
			$info = $file->move(ROOT_PATH.'public'.DS.'uploads');
			if($info)
			{
				//成功上传后获取信息
				$img_src = '/uploads/'.$info->getSaveName();
				echo $file->getError();
			}

		}else
		{
			$img_src = '/uploads/20181015\4a03632b430cbc0bc385ddfc6a172c94.jpg';
		}
		$rule = [
			'note_title'		=> 'require',
			'note_content'		=> 'require',
			'note_label'		=> 'require',
		];

		$msg = [
			'note_title.require' 		=>  '标题不能为空',
			'note_content.require'		=>  '内容不能为空',
			'note_label.require'		=>  '必须选择标签'
		];

		$data = [
			'note_title'		=> $note_title,
			'note_content'		=> $note_content,
			'note_label'		=> $note_label,
		];

		$validate = new Validate($rule,$msg);
		$result = $validate->check($data);

		if( ! $result)
		{
			$this->error($validate->getError());
		}

		else{
		$data = [
			'user_id'  		    =>  session('user_id'),
			'note_title'		=>  $note_title,
			'note_content'		=>  $note_content,
			'note_label'		=>  $note_label,
			'note_himg'			=>  $img_src,
			'addtime'  			=>  time(),
		];

		}	
		
 		$ret = db('note')->insert($data);
 		if( $ret ==false )
 		{
 			$this->error("提交失败");
 		}
 		$this->success("提交成功",'/home/community/index');	
	}

	public function delnote()
	{
		$note_id = input('get.id');
		$ret = db('note')->where("note_id={$note_id}")->delete();
		if( $ret ==false )
 		{
 			$this->error("删除失败",'/home/note/myNote');
 		}
 		$this->success("删除成功",'/home/note/myNote');
	}

	public function detail()   //帖子或者新闻内容
	{
		$id = input('get.id');
		$user_id = session('user_id');
		$type = input('get.type');
		if($type!=NULL&&$type=='new'){
			$iddata = db('new')->where("new_id={$id}")->find();
			$ret = db('new')->alias('a')->join('admin b','a.admin_id=b.admin_id')->where("a.new_id={$id}")->field('a.*,b.admin_name,b.admin_himg')->find();
		}else{
			$iddata = db('note')->where("note_id={$id}")->find();
			$ret = db('note')->alias('a')->join('user b','a.user_id=b.user_id')->where("a.note_id={$id}")->field('a.*,b.user_name,b.user_img')->find();
		}
		if( $user_id != NULL)
		{
			$result = db('user')->where("user_id={$user_id}")->find();
		}
		else
		{
			$user_id = "";
			$result = ['user_name'=>''];
		}
		$data = db('focusnote')->where("user_id = '{$user_id}' and note_id = '{$id}'")->find();	//判断当前用户是否关注了该帖子
		if($data!=null){
			$is_focus = 1;
		}else{
			$is_focus = 0;
		}

		if(!empty($user_id)){
    		$result  = db("user")->where("user_id = {$user_id}")->find();
    	}
    	else{
    		$user_id="";
    		$result=[];
            $result['user_name']="普通用户";
        }

		//点赞量
		$like_num = db('like')->where("id={$id}")->count("like_id");

		//更新阅读量
		if($type!=NULL&&$type=='new'){
			$result2  = db("new")->where("new_id = {$id}")->setInc("new_visit");
		}else{
			$result2  = db("note")->where("note_id = {$id}")->setInc("note_visit");
		}

		$hot_list = db("note")->where("note_status = 1")->limit(5)->order('note_visit desc')->select();      //最热帖子列表
        $lastest_list = db("note")->where("note_status = 1")->order("addtime desc")->limit(6)->select();  //最新帖子列表
        $new_list = db("new")->order("addtime desc")->limit(5)->select();       //新闻列表
        $this->assign("hot_list",$hot_list);
        $this->assign("new_list",$new_list);
        $this->assign("lastest_list",$lastest_list);

		$comment_list = db('comment')->alias('c')->join("user u","c.user_id=u.user_id")->where("be_id = {$id}")->select();
		$this->assign('comment_list',$comment_list);
		$retype_list = db('new_report_type')->select();
		$this->assign('user_id',$user_id);
		$this->assign('type',$type);
		$this->assign('like_num',$like_num);
		$this->assign('is_focus',$is_focus);
		$this->assign('result',$result);
		$this->assign('retype_list',$retype_list);
		$this->assign('iddata',$iddata);
		$this->assign('ret',$ret);
		return view('detail');
	}

	public function publish(){
		//验证登录代码
        if ( session('user_id') == NULL)
        {	
        		session('url', $_SERVER['PHP_SELF']);
	        	$this->error("未登录",'home/login/login');
		   }
		   
		if(!empty($user_id)){
    		$result  = db("user")->where("user_id = {$user_id}")->find();
    	}
    	else{
    		$user_id="";
    		$result=[];
            $result['user_name']="普通用户";
		}

		$hot_list = db("note")->where("note_status = 1")->limit(5)->order('note_visit desc')->select();      //最热帖子列表
        $lastest_list = db("note")->where("note_status = 1")->order("addtime desc")->limit(6)->select();  //最新帖子列表
        $new_list = db("new")->order("addtime desc")->limit(5)->select();       //新闻列表
        $this->assign("hot_list",$hot_list);
        $this->assign("new_list",$new_list);
        $this->assign("lastest_list",$lastest_list);
        $this->assign("result",$result);
		$small_list = db('plate')->where('plate_type = 2')->select();
		$this->assign('small_list',$small_list);
		return view('publish');
	}

	public function giveLike(){	//点赞方法
		$id = input('post.id');
		$type = input('post.type');
		$user_id = session('user_id');
		$current_time = time()-60*60*24;
		$result = db('like')->where("user_id = {$user_id} and id={$id} and type = {$type} and addtime>='{$current_time}'")->find();
		if($result){
			return [
				'msg'   => '你今天已经点过赞了',
				'status'=> 0
			];
		}
		$data = [
			'id' 	 => $id,
			'type'	 => $type,
			'user_id'=> $user_id,
			'addtime'=> time()
		];
		$ret = db('like')->insert($data);
		if($ret){
			return [
				'msg'   => '点赞成功',
				'status'=> 1
			];
		}else{
			return [
				'msg'   => '点赞失败',
				'status'=> 0
			];
		}
	}

	public function confirmReport(){
		$content = input('post.content');
		$retype_name = input('post.retype_name');
		$id = input('post.id');
		$type = input('post.type');
		$user_id = session('user_id');
		if($type==1){	//举报的是帖子
			$data = [
				'content'  => $content,
				'type'	   => $retype_name,
				'note_id'  => $id,
				'user_id'  => $user_id,
				'is_accept'=> 0,
				'addtime'  => time()
			];
			$ret = db('notereport')->insert($data);
			if($ret){
				return [
					'msg'   => '举报成功',
					'status'=> 1
				];
			}else{
				return [
					'msg'   => '举报失败',
					'status'=> 0
				];
			}
		}else{			//举报的是新闻
			$data = [
				'content'  => $content,
				'type'	   => $retype_name,
				'new_id'   => $id,
				'user_id'  => $user_id,
				'is_accept'=> 0,
				'addtime'  => time()
			];
			$ret = db('newreport')->insert($data);
			if($ret){
				return [
					'msg'   => '举报成功',
					'status'=> 1
				];
			}else{
				return [
					'msg'   => '举报失败',
					'status'=> 0
				];
			}
		}
	}

	public function uploadImg(){
		$file = request()->file('file');
        $img_src = "";
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
				// 输出 jpg
				$ret['code'] = 1;
				$ret['msg']  = 'success';
				$ret['src'] = '/uploads/'.$info->getSaveName();
				return json_encode($ret);
            }
            else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
	}

	public function add_note(){
		$content 	= input('post.content');
		$title 		= input('post.title');
		$plate_id 	= input('post.plate_id');
		$himg 		= input('post.himg');
		$user_id 	= session('user_id');
		$data = [
			'content'  => $content,
			'title'	   => $title,
			'plate_id' => $plate_id,
			'himg'     => $himg,
			'user_id'  => $user_id,
			'addtime'  => time()
		];
		$ret = db('note')->insert($data);
		if($ret){
			$this->success("发帖成功");
		}else{
			$this->error("发帖失败");
		}
	}

	public function focusnote(){
		$note_id = input('post.note_id');
		$user_id = session('user_id');
		$data = [
			'note_id'  => $note_id,
			'user_id'  => $user_id,
			'addtime'  => time()
		];
		$ret = db('focusnote')->insert($data);
		if($ret){
			return [
				'msg'   => '关注成功',
				'status'=> 1
			];
		}else{
			return [
				'msg'   => '关注失败',
				'status'=> 0
			];
		}
	}

	public function delfocusnote(){
		$note_id = input('post.note_id');
		$user_id = session('user_id');
		$ret = db('focusnote')->where("user_id = {$user_id} and note_id = {$note_id}")->delete();
		if($ret){
			return [
				'msg'   => '取消关注成功',
				'status'=> 1
			];
		}else{
			return [
				'msg'   => '取消关注失败',
				'status'=> 0
			];
		}
	}

	public function add_apply(){
		$plate_id = input('post.plate_id');
		$apply_content = input('post.apply_content');
		$user_id = session('user_id');
		$data = [
			'plate_id'  	 => $plate_id,
			'apply_content'  => $apply_content,
			'user_id'  		 => $user_id,
			'addtime'  		 => time()
		];
		$ret = db('apply')->insert($data);
		if($ret){
			$this->success("你已成功提交申请");
		}else{
			$this->error("提交申请失败");
		}
	}

	public function giveComment(){
		$comment_content = input('post.comment_content');
		$id 	 = input('post.id');
		$user_id = session('user_id');
			$data = [
				'comment_content'  	=> $comment_content,
				'be_id'  			=> $id,
				'user_id' 	 		=> $user_id,
				'addtime'  			=> time()
			];
			$ret = db('comment')->insert($data);
			if($ret){
				return [
					'msg'   => '评论成功',
					'status'=> 1
				];
			}else{
				return [
					'msg'   => '评论失败',
					'status'=> 0
				];
			}
	}
}